package org.itstack.demo.design;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author: fanYuLe
 * @CreateTime: 2023-12-15  15:36
 * @Description: CAS[AtomicReference] （线程安全）
 */
public class Singleton_06 {

    private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<>();

    private static Singleton_06 instance;

    private Singleton_06(){}

    public static final Singleton_06 getInstance(){
        for (;; ) {
            Singleton_06 instance = INSTANCE.get();
            if(null!=instance){return instance;}
            INSTANCE.compareAndSet(null,new Singleton_06());
            return INSTANCE.get();
        }
    }

    public static void main(String[] args) {
        System.out.println(Singleton_06.getInstance());//org.itstack.demo.design.Singleton_06@e2d56bf
        System.out.println(Singleton_06.getInstance());//org.itstack.demo.design.Singleton_06@e2d56bf
    }
}
